package com.yakumosakura.yakumoblade.event;

import cn.mmf.slashblade_addon.SlashBladeAddon;
import cn.mmf.slashblade_addon.registry.SBASlashArtsRegistry;
import com.exfantasycode.mclib.Utils.SlashBlade.SlashEffectUtils;
import com.yakumosakura.yakumoblade.Yakumoblade;
import com.yakumosakura.yakumoblade.registry.slashblade.YASlashArtRegistry;
import com.yakumosakura.yakumoblade.registry.slashblade.YASpecialEffectsRegistry;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.SlashArtsRegistry;
import mods.flammpfeil.slashblade.slasharts.SlashArts;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

import static com.yakumosakura.yakumoblade.Yakumoblade.MODID;
import static com.yakumosakura.yakumoblade.Yakumoblade.prefix;
import static tfar.classicbar.ClassicBar.logger;

@Mod.EventBusSubscriber
public class ChangeType {
    public static void execute(Entity entity) {
        if (entity == null)
            return;
        if (entity instanceof Player _player && !_player.level().isClientSide()){
            if ((entity instanceof LivingEntity v) ){
//                changehexgramfox(_player,entity,v);
//                changehexgramdragon(_player,entity,v);
            }
//            _player.displayClientMessage(Component.literal("\u6309\u4E0B\u4E86\u6848\u4EF6"), false);

        }
    }
    public static Boolean colcok=true;




//    public static void changehexgramfox(Player _player,Entity entity,LivingEntity _livEnt){
//        SlashArts sa=  getsa(_player);
//        if ( SlashEffectUtils.hasSpecialEffect(_livEnt.getMainHandItem(),MODID+":"+ YASpecialEffectsRegistry.HexGamFox.getId().getPath())){
//            if(gettext(_player).get().equals(prefix("model/named/custom/hexagram/foxhexagram.png"))||sa==YASlashArtRegistry.FoxSs.get()){
//                System.out.println("成功了");
//                _player.displayClientMessage(Component.literal(createis(_player,YASlashArtRegistry.FoxSsEX.getId())), true);
//                setsa(_player,YASlashArtRegistry.FoxSsEX.getId());
//                settext(_player,"model/named/custom/hexagram/foxhexagram_kb.png");
//            }else if(gettext(_player).get().equals(prefix("model/named/custom/hexagram/foxhexagram_kb.png"))||sa==YASlashArtRegistry.FoxSsEX.get()){
//                setsa(_player,YASlashArtRegistry.FoxSs.getId());
//                _player.displayClientMessage(Component.literal(createis(_player,YASlashArtRegistry.FoxSs.getId())), true);
//                System.out.println("成功了");
//                settext(_player,"model/named/custom/hexagram/foxhexagram.png");
//            }
//        }
//    }
//    public static void changehexgramdragon(Player _player,Entity entity,LivingEntity _livEnt){
//        SlashArts sa=  getsa(_player);
//        if ( SlashEffectUtils.hasSpecialEffect(_livEnt.getMainHandItem(),MODID+":"+ YASpecialEffectsRegistry.HexGamDragon.getId().getPath())){
//            if(gettext(_player).get().equals(prefix("model/named/custom/hexagram/dragonhexagram.png"))||sa==YASlashArtRegistry.Dragomss.get()){
//                System.out.println("成功了");
//                _player.displayClientMessage(Component.literal(createis(_player,YASlashArtRegistry.Dragomssex.getId())), true);
//                setsa(_player,YASlashArtRegistry.Dragomssex.getId());
//                settext(_player,"model/named/custom/hexagram/dragonhexagram_kb.png");
//            }else if(gettext(_player).get().equals(prefix("model/named/custom/hexagram/dragonhexagram_kb.png"))||sa==YASlashArtRegistry.Dragomssex.get()){
//                setsa(_player,YASlashArtRegistry.Dragomss.getId());
//                _player.displayClientMessage(Component.literal(createis(_player,YASlashArtRegistry.Dragomss.getId())), true);
//                System.out.println("成功了");
//                settext(_player,"model/named/custom/hexagram/dragonhexagram.png");
//            }
//        }
//    }

    public static Optional<ResourceLocation> gettext(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getTexture()).get();
    }
    public static void settext(Player playerIn,String text){
        playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .ifPresent(state -> state.setTexture(Yakumoblade.prefix(text)));
    }
    public static SlashArts getsa(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getSlashArts()).get();
    }
    public static void setsa(Player playerIn, ResourceLocation sa) {
        if (playerIn == null) {
            logger.warn("Player is null, cannot set slash arts key.");
            return;
        }

        ItemStack mainHandItem = playerIn.getMainHandItem();
        if (mainHandItem.isEmpty()) {
            logger.warn("Player's main hand item is empty, cannot set slash arts key.");
            return;
        }

        @NotNull LazyOptional<ISlashBladeState> capability = mainHandItem.getCapability(ItemSlashBlade.BLADESTATE);
        capability.ifPresent(state -> {
            try {
                state.setSlashArtsKey(sa);
            } catch (Exception e) {
                logger.error("Failed to set slash arts key", e);
            }
        });

        if (!capability.isPresent()) {
            logger.warn("Item does not have BLADESTATE capability, cannot set slash arts key.");
        }
    }
    public static String gettran(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getTranslationKey()).get();
    }
    public static String gettransa(ResourceLocation sa){
        return "slash_art."+sa.toLanguageKey();
    }
    public static String createis(Player _player,ResourceLocation sa){
        return Component.translatable(gettran(_player)).getString()
                +Component.translatable("yakumoblade.types.open").getString()
                +Component.translatable(gettransa(sa)).getString();
    }
}
